import axios from "axios";

import qs from "qs";
import { Message } from "element-ui";
import router from "../router/index";
// const METHOD = {
//     // URL: "	http://127.0.0.1:4523/mock/374473"
// URL:"https://www.bangbangtongcheng.com"
// }
const newSource = {};
// 构建axios实例
const instance = axios.create({
  // baseURL: "https://cp.hbcpre.com/bqdj/",
  // baseURL: "https://conven.lefenglian.top/conven/",
  // baseURL: "https://ws.weisou.online/conven/",

  // baseURL: "http://15771110980.gnway.cc:80/conven/",
  // baseURL: "https://e530t35482.zicp.fun/conven/",
  // baseURL: "https://465b911d.r7.cpolar.top/conven/",
  // baseURL: "https://yuzd.lefenglian.top/conven/",
  // baseURL: "http://192.168.0.112:8083/conven/",
  // baseURL: "http://wk65v6.natappfree.cc/conven/",

  baseURL: "https://www.bangbangtongcheng.com/conven/",
  // baseURL: "https://pingduwanghong.lefenglian.top/conven/",
  // baseURL: "http://192.168.0.111:8083/conven/",
  // baseURL:"https://weisou.mynatapp.cc/conven/",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
    // 'content-type': 'multipart/form-data'
  }
  // timeout: 5000  // 设置请求超时连接时间
});

// 请求拦截
instance.interceptors.request.use(
  config => {
    // 指定请求令牌
    if (localStorage.getItem("token")) {
      config.headers["Token"] = localStorage.getItem("token");
    }
    // // 自定义令牌的字段名为X-Token，根据咱们后台再做修改
    return config;
  },
  error => {
    // 请求错误的统一处理
    // console.log(error); // for debug
    return Promise.reject(error);
  }
);

// // 响应拦截器
instance.interceptors.response.use(
  /**
   * 通过判断状态码统一处理响应，根据情况修改
   * 同时也可以通过HTTP状态码判断请求结果
   */
  response => {
    const res = response.data;
    // console.log('response',res);
    //异地登录
    // if(res.code==-1){
    //     router.push("/login");
    // }
    // 如果状态码不是200则认为有错误
    if (!res.success) {
      Message.error({
        message: res.msg || "Error",
        duration: 2 * 1000
      });

      return Promise.reject(new Error(res));
    } else {
      return res;
    }
  },
  error => {
    // error为{}
    if (error.message) {
      Message({
        message: error.message,
        type: "error",
        duration: 20 * 1000
      });
    }
    // alert('错误')
    return Promise.reject(error);
  }
);

// * 网络请求
// * @param method 方法
// * @param url 接口地址
// * @param params 参数
// * @param showError 是否展示错误信息
// * @returns {Promise<any>}
async function getHttp(url, params, showError, option = {}) {
  if (showError || showError == undefined) {
    // 是否展示错误信息
    showError = false;
  } else {
    showError = false;
  }
  if (option.iscancel) {
    if (typeof newSource[option.iscancel] === "function") {
      newSource[option.iscancel]();
    }
    option.cancelToken = new axios.CancelToken(
      c => (newSource[option.iscancel] = c)
    );
  }
  return new Promise((resolve, reject) => {
    instance({
      url: url,
      method: "get",
      params: params,
      ...option
    })
      .then(res => {
        // console.log(res)
        if (res.code==200) {
          // 200 是请求成功
          resolve(res);
        } else {
          // 其他情况返回错误信息，根据需要处理
          reject(res);
          if (showError) {
            Message.error(res.msg);
          }
        }
      })
      .catch(error => {
        reject(error);
        // if (showError) {
        //     Message.error('请求失败，请稍后再试');
        // }
      });
  });
}

async function postHttp(url, params, showError, config) {
  if (showError || showError == undefined) {
    // 是否展示错误信息
    showError = true;
  } else {
    showError = false;
  }
  
  if (!config) {
    var obj = {
      url: url,
      method: "post",
      data: qs.stringify(params),
      // params: qs.stringify(params),

      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    
      }
    };
    

    
  } else {
    var obj = {
      url: url,
      method: "post",
      data: params,
    };
    obj.headers={
      "Content-Type":config['contentType']||"application/x-www-form-urlencoded"
    }
  }

  return new Promise((resolve, reject) => {
    instance({
      // url: url,
      // method: 'post',
      // data: qs.stringify(params),
      // headers: {
      //     'Content-Type': 'application/x-www-form-urlencoded'
      // }
      ...obj
    })
      .then(res => {
        // console.log(res.code)
        if (res.code == 200) {
          // 200 是请求成功
          // console.log('picpipcpic')
          resolve(res);
        } else {
          // 其他情况返回错误信息，根据需要处理
          reject(res);
          if (showError) {
            if (res.hasOwnProperty("msg")) {
              Message.error(res.msg);
            } else {
              Message.error(res.data.msg);
            }
          }
        }
      })
      .catch(error => {
        reject(error);
        // if (showError) {
        //     Message.error('请求失败，请稍后再试');
        // }
      });
  });
}

function install(Vue) {
  // Vue.prototype.imgUrl = 'https://www.bangbangtongcheng.com/zas/upload/'
  Vue.prototype.$getHttp = getHttp;
  Vue.prototype.$postHttp = postHttp;
  // Vue.prototype.$imgUrl = "https://hqww.cn.utools.club/bqdj/upload/";
  // Vue.prototype.$url = "/api";
  // Vue.prototype.$upLoad = "https://hqww.cn.utools.club/bqdj/pc/attach/upload"
  //Vue.prototype.$img = "https://www.bangbangtongcheng.com/conven/upload";
  Vue.prototype.$img = "";
  Vue.prototype.$imgUrl = "https://www.bangbangtongcheng.com/conven/upload/";
  Vue.prototype.$upLoad =
    "https://www.bangbangtongcheng.com/conven/ht/attach/qnUpload";
  Vue.prototype.$url = "https://www.bangbangtongcheng.com/bqdj/";

  //简历魔板的url
  Vue.prototype.$resmeUpload = "https://ws.weisou.online/uploads/";
  Vue.prototype.$resmeUpload = "https://www.bangbangtongcheng.com/upload/";


  Vue.prototype.$basePageUrl = "https://ws.weisou.online/conven_web/#/";
  Vue.prototype.$basePageUrl = "https://www.bangbangtongcheng.com/#/";
  // Vue.prototype.$imgUrl = 'https://conven.lefenglian.top/conven/upload/'
  // Vue.prototype.$upLoad = "https://conven.lefenglian.top/conven/ht/attach/qnUpload"
  // Vue.prototype.$imgUrl = 'https://ws.weisou.online/conven/upload/'
  // Vue.prototype.$upLoad = "https://ws.weisou.online/conven/ht/attach/qnUpload"
  if (process.env.NODE_ENV === "development") {
    Vue.prototype.$baseURL = "https://ws.weisou.online/conven/";
    // Vue.prototype.$baseURL = "http://192.168.0.108:8083/conven/";
    Vue.prototype.$baseURL = "https://www.bangbangtongcheng.com/conven/";
    // Vue.prototype.$baseURL = "https://www.bangbangtongcheng.com/conven/";
  } else {
    Vue.prototype.$baseURL = "https://ws.weisou.online/conven/";
    Vue.prototype.$baseURL = "https://www.bangbangtongcheng.com/conven/";
  }

  // Vue.prototype.$imgUrl = 'http://wk65v6.natappfree.cc/conven/upload/'
  // Vue.prototype.$upLoad = "http://wk65v6.natappfree.cc/conven/ht/attach/qnUpload"
}
export default install;
